﻿@{
    ViewBag.TitlePrefix = "Tags";
}

@using System.Web
@model IReadOnlyList<Moonglade.Model.DegreeTag>

@functions {
    private static int Weight(double weightPercent)
    {
        int weight;
        if (weightPercent >= 99)
        {
            weight = 1;
        }
        else if (weightPercent >= 70)
        {
            weight = 2;
        }
        else if (weightPercent >= 40)
        {
            weight = 3;
        }
        else if (weightPercent >= 20)
        {
            weight = 4;
        }
        else if (weightPercent >= 3)
        {
            weight = 5;
        }
        else
        {
            weight = 0;
        }
        return weight;
    }
}

@if (null != ViewBag.IsServerError && ViewBag.IsServerError)
{
    <partial name="_ServerErrorMessage" />
}
else
{
    <h3>
        @Localizer["Tags"]
    </h3>
    <hr />

    if (Model.Any())
    {
        var tagMax = Model.Select(p => p.Degree).Max();

        <ul class="list-unstyled tagCloud">
            @foreach (var tag in Model.OrderBy(t => t.DisplayName))
            {
                var weightPercent = Convert.ToInt32(tag.Degree) * 1.0 / Convert.ToInt32(tagMax) * 100;
                var weight = Weight(weightPercent);

                if (weight >= 0)
                {
                    <li class="d-inline-block">
                        <a asp-controller="Tags"
                           asp-action="List"
                           asp-route-normalizedName="@HttpUtility.UrlDecode(tag.NormalizedName.ToLower())"
                           class="weight@(weight) d-block mr-2" title="@(tag.Degree)">
                            @tag.DisplayName.Replace("-", " ")
                        </a>
                    </li>
                }
            }
        </ul>
    }
    else
    {
        <div class="alert alert-info">
            @Localizer["No Tags"]
        </div>
    }
}

<partial name="_LightSwitch" />